<template>
  <div ref="imageWrapper">
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form-model ref="form" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <!-- <a-col :md="4" :sm="6">
            <a-form-model-item label="计划类型">
              <j-dict-select-tag
                style="width: 100%"
                v-model="queryParam.planType"
                placeholder="请选择"
                dict-code="inconplantype"
              />
            </a-form-model-item>
          </a-col> -->
            <a-col :md="5" :sm="6">
              <a-form-model-item label="账套/核算单元" prop="accName">
                <a-input @click.native="openOrganModel" v-model="queryParam.accName" readOnly
                  placeholder="请选择账套/核算单元" />
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="6">
              <a-form-model-item label="开始时间" prop="accStartTime">
                <a-date-picker format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="请选择开始时间"
                  v-model="queryParam.accStartTime" />
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="6">
              <a-form-model-item label="结束时间" prop="accEndTime">
                <a-date-picker format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="请选择结束时间"
                  v-model="queryParam.accEndTime" />
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="6">
              <a-form-model-item label="摘要" prop="">
                <a-input placeholder="请输入摘要" v-model="queryParam.summary" />
              </a-form-model-item>
            </a-col>
            <a-col :md="7" :sm="8">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
              <a-button type="danger" :loading="ydLoading" @click="
                () => {
                  if (this.dataSource.length) {
                    const { accYear = '', accStartTime = '', accEndTime = '', summary = '' } = this.queryParam
                    let ydnr = `【摘要：${summary}】`
                    let time = `【${accStartTime}~${accEndTime}】【摘要汇总分析】`
                    this.addYd(queryParam, ydnr, time)
                  } else {
                    this.$message.error('空数据不能新建疑点')
                  }
                }
              " icon="arrow-right" style="margin-left: 8px">转疑点</a-button>
              <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                返回
              </a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <a-table ref="table" size="middle" bordered :columns="columns" :dataSource="dataSource" :pagination="ipagination"
        :loading="loading" @change="handleTableChange">
        <span slot="summary" slot-scope="text">
          <j-ellipsis :value="text" :length="13" />
        </span>
        <template v-slot:unitName="text">
          <j-ellipsis :value="text" :length="13"></j-ellipsis>
        </template>
        <template v-slot:accName="text">
          <j-ellipsis :value="text" :length="13"></j-ellipsis>
        </template>
        <template v-slot:accStartTime="text">
          <span>{{ text | TimerFormat }}</span>
        </template>
        <template v-slot:accEndTime="text">
          <span>{{ text | TimerFormat }}</span>
        </template>
        <!-- <span slot="action" slot-scope="text, record">
          <a @click="addYd(record)">转疑点</a>
        </span> -->
      </a-table>
      <CheckOrganModal ref="departModal" @ok="updateQueryParam"></CheckOrganModal>
      <YDModel ref="YDModel" :routerData="$route.path" />
    </a-card>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import moment from 'moment'
// import detailModel from './searchResult/index'
import CheckOrganModal from '@/views/sj/sjxmzygl/yzzx/testcomponent/CheckOrganModal'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '@/views/sj/components/YDModel'
import { beforeyearYMD, beforeyearY, currentYMD } from '@/mixins/commontime.js'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
export default {
  name: 'ss',
  mixins: [JeecgListMixin, base64toImg, jumpConfigMixin],
  components: { CheckOrganModal, YDModel },
  data() {
    return {
      // 表头
      queryParam: {
        accName: '',
        accSetId: '',
        unitId: '',
        accYear: '',
        accStartTime: beforeyearYMD(2, 'years'),
        accEndTime: currentYMD(),
      },
      isBackButton: false,
      options: [
        { label: '贷方', value: -1 },
        { label: '借方', value: 1 },
      ],
      rules: {
        accYear: [{ required: true, message: '请选择年度' }],
        accStartTime: [{ required: true, message: '请选择开始时间' }],
        accEndTime: [{ required: true, message: '请选择结束时间' }],
        accName: [{ required: true, message: '请选择账套' }],
      },
      columns: columns(this),
      url: {
        list: '/sjkmfx/sjSubjectsAnalyse/querySummaryDetail',
      },
      yearPickShow: false,
      dataSource: [],
      disableMixinCreated: true,
    }
  },
  filters: {
    TimerFormat(time) {
      return time.indexOf(' ') ? time.split(' ')[0] : time
    },
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          this.queryParam = Object.assign(this.queryParam, to.query)
          this.$nextTick(() => {
            this.searchQuery()
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          let { accName, accId } = to.params
          this.queryParam.accName = accName
          this.queryParam.accCode = accId

          let {endDate,startDate} = JSON.parse(to.params.returnRecord)
          this.queryParam.accStartTime = startDate
          this.queryParam.accEndTime = endDate

          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
          this.$nextTick(() => {
            this.loadData()
          })
        }
      },
      immediate: true,
    },
  },
  methods: {
    searchReset() {
      this.queryParam = this.$options.data().queryParam
      this.dataSource = []
      this.ipagination = this.$options.data().ipagination
    },
    searchQuery() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loadData(1)
        } else {
          return false
        }
      })
    },
    updateQueryParam(data) {
      console.log(data, 'daat')
      let str1 = ''
      let str2 = ''
      let str3 = ''
      data.map((item) => {
        // TODO 账套～～～
        str1 += item.accCode + ','
        str2 += item.accName + ','
        str3 += item.unitId + ','
      })
      if (str1.length > 0) {
        str1 = str1.substr(0, str1.length - 1)
      }
      if (str2.length > 0) {
        str2 = str2.substr(0, str2.length - 1)
      }
      if (str3.length > 0) {
        str3 = str3.substr(0, str3.length - 1)
      }
      this.queryParam.accSetId = str1
      this.queryParam.accName = str2
      this.queryParam.unitId = str3
    },
    openOrganModel() {
      this.$refs.departModal.edit()
    },
    handleAddSubmit(evt) {
      return new Promise((re, rj) => {
        postAction(this.url.add, evt).then((res) => {
          if (res.success) {
            this.$refs.table.loadData()
            this.$message.success(res.message)
            re(res.success)
          } else {
            rj(res.success)
            this.$message.error(res.message)
          }
        })
      })
    },
    handlePanelChange(value) {
      this.queryParam.accYear = moment(value).format('YYYY')
      this.yearPickShow = false
      this.$refs.form.validateField('accYear', (error) => {
        console.log(error)
      })
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
    },
    handleViewDetail(row) {
      this.$refs.detailModel.getPlanId(row)
      this.$refs.detailModel.visible = true
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
</style>